package com.example.jsonarry.nanningtravel.Utils;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Parcelable;
import android.util.Log;
import android.widget.Toast;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by jsonarry on 2015/12/1.
 */
public class Utils
{

    private static final String TAG = "Utils";

    //    case ID_QQWEIBO:
//    initShareIntent("com.tencent.wblog");
//    break;
//    case ID_WEIXIN:
//    initShareIntent("com.tencent.mm");
//    break;
//    case ID_EVERNOTE:
//    initShareIntent("evernote");
//    break;
//    case ID_SINAWEIBO:
//    initShareIntent("com.sina.weibo");
//    break;
//    case ID_RENREN:
//    initShareIntent("renren");
//    break;
//    case ID_QQ:
//    initShareIntent("tencent.mobileqq");
//    break;
    public static void shareImage(Context context, String type) {
        boolean found = false;
        Intent share = new Intent(android.content.Intent.ACTION_SEND);
        share.setType("text/plain");
//        share.setType("image/jpeg");
        // gets the list of intents that can be loaded.
        List<ResolveInfo> resInfo = context.getPackageManager().queryIntentActivities(share, 0);
        if (!resInfo.isEmpty()){
            for (ResolveInfo info : resInfo) {
               String packName = info.activityInfo.packageName;
                if (packName.toLowerCase().contains(type) ||
                        packName.toLowerCase().contains(type) ) {
                    share.putExtra(Intent.EXTRA_SUBJECT,  "subject");
                    share.putExtra(Intent.EXTRA_TEXT,     "your text");
                    // Optional, just if you wanna share an image.
//                    share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(myPath)) );
                    share.setPackage(packName);
                    Log.e(TAG, "shareImage: packName----------"+packName);
//                    found = true;
//                    break;
                }
            }
//            if (!found)
//                return;
            context.startActivity(Intent.createChooser(share, "Select"));
        }
    }
   public static void shareText(Context context,String[] type)
   {
       String contentDetails = "";
       String contentBrief = "";
       String shareUrl = "";
       Intent it = new Intent(Intent.ACTION_SEND);
       it.setType("text/plain");
       List<ResolveInfo> resInfo = context.getPackageManager().queryIntentActivities(it, 0);
       if(!resInfo.isEmpty())

       {
           List<Intent> targetedShareIntents = new ArrayList<Intent>();
           for (ResolveInfo info : resInfo)
           {
               Intent targeted = new Intent(Intent.ACTION_SEND);
               targeted.setType("text/plain");
               ActivityInfo activityInfo = info.activityInfo;
               // judgments : activityInfo.packageName, activityInfo.name, etc.
               if (activityInfo.name.contains(type[0]) || activityInfo.name.contains(type[1])||
                       activityInfo.name.contains(type[2]))
               {
                   targeted.putExtra(Intent.EXTRA_SUBJECT,  "subject");
                   targeted.putExtra(Intent.EXTRA_TEXT,     "your text");
                   targeted.setPackage(activityInfo.packageName);
                   Log.e(TAG, "shareText:--- "+activityInfo.packageName+"==="+activityInfo.name );

                   targetedShareIntents.add(targeted);
               }

           }
           Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), "Select app to share");
           if (chooserIntent == null)
           {
               return;
           }
           // A Parcelable[] of Intent or LabeledIntent objects as set with
           // putExtra(String, Parcelable[]) of additional activities to place
           // a the front of the list of choices, when shown to the user with a
           // ACTION_CHOOSER.
           chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
           try
           {
               context.startActivity(chooserIntent);
           }
           catch (android.content.ActivityNotFoundException ex)
           {
               Toast.makeText(context, "Can't find share component to share", Toast.LENGTH_SHORT).show();
           }
       }
   }
}
